/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.fonzie;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Fonzie
{
    private static EntityManagerFactory emf;

    private static QueryParser parser;

    private static Logger logger = LoggerFactory.getLogger( Fonzie.class );

    public static void configure( EntityManagerFactory emf )
    {
        configure( emf, new DefaultQueryParser() );
    }

    public static void configure( EntityManagerFactory emf, QueryParser parser )
    {
        Fonzie.emf = emf;
        Fonzie.parser = parser;
    }

    public static void persist( Object entity )
    {
        logger.debug( "persist entity {}", entity );
        emf.createEntityManager().persist( entity );
    }

    public static void remove( Object entity )
    {
        logger.debug( "remove entity {}", entity );
        emf.createEntityManager().remove( entity );
    }

    public static void refresh( Object entity )
    {
        logger.debug( "refresh entity {}", entity );
        emf.createEntityManager().refresh( entity );
    }

    public static void detach( Object entity )
    {
        logger.debug( "detach entity {}", entity );
        emf.createEntityManager().detach( entity );
    }

    public static boolean isManaged( Object entity )
    {
        logger.debug( "check if entity {} is managed", entity );
        return emf.createEntityManager().contains( entity );
    }

    public static <T> T getReference( Class<T> entity, Object id )
    {
        logger.debug( "getReference for entity {} with ID", entity.getSimpleName(), id );
        return emf.createEntityManager().getReference( entity, id );
    }

    public static <T> T find( Class<T> entity, Object id )
    {
        logger.debug( "find entity {} with ID", entity.getSimpleName(), id );
        return emf.createEntityManager().find( entity, id );
    }

    public static <T> List<T> find( Class<T> entity, String criteria, Object args )
    {
        logger.debug( "find entity {} by {} ", entity.getSimpleName(), criteria );
        String jpql = parser.parse( criteria );
        TypedQuery<T> q = emf.createEntityManager().createQuery( jpql, entity );
        return q.getResultList();
    }

    public static <T> T findSingle( Class<T> entity, String criteria, Object args )
    {
        logger.debug( "find entity {} by {} ", entity.getSimpleName(), criteria );
        String jpql = parser.parse( criteria );
        TypedQuery<T> q = emf.createEntityManager().createQuery( jpql, entity );
        return q.getSingleResult();
    }
}
